package com.kevin.miniospring;

import com.kevin.minio.util.FileUploadClient;
import com.kevin.minio.util.FileUploadClientFactory;
import lombok.Setter;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;

import java.util.Arrays;

import static org.springframework.util.Assert.notNull;

/**
 * @author Kevin Liu
 * @date 2020/6/10 9:19 上午
 */
@Setter
public class BucketRegister implements InitializingBean, BeanFactoryPostProcessor {

    private FileUploadClientFactory fileUploadClientFactory;

    private String [] buckets;

    public BucketRegister(){

    }

    public BucketRegister(FileUploadClientFactory fileUploadClientFactory) {
        this.fileUploadClientFactory = fileUploadClientFactory;
    }



    @Override
    public void afterPropertiesSet() throws Exception {
        notNull(this.buckets, "Property 'buckets' is required");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        for (String bucket : buckets) {
            String beanName = bucket + FileUploadClient.class.getSimpleName();
            configurableListableBeanFactory.registerSingleton(beanName,this.fileUploadClientFactory.openFileUploadClient(bucket));
        }
    }
}
